package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.ProjrectProgressBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProjectProgressActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private String budget_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_progress);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("项目进度");
        budget_id = getIntent().getStringExtra("budget_id");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    @Override
    protected void onResume() {
        upLoad();
        super.onResume();
    }

    private List<ProjrectProgressBean.InfoEntity> info1 = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        if (budget_id == null) {
            MyToast("套餐错误");
            return;
        }
        map.put("budget_id", budget_id);
        showDialog();
        XUtil.Post(Url.BUDGET_GETBUDGETPROGRESSLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        ProjrectProgressBean pro = gson.fromJson(result, ProjrectProgressBean.class);
                        info1 = pro.getInfo();
                        initData();
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private void initData() {
        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_progress;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (Integer.parseInt(info1.get(position).getType())) {
                            case 1:
                                startActivity(new Intent(ProjectProgressActivity.this, ReviewActivity.class).putExtra("budget_id", budget_id));
                                break;
                            case 2:
                                startActivity(new Intent(ProjectProgressActivity.this, InListActivity.class).putExtra("budget_id", budget_id));
                                break;
                            case 3:
                                startActivity(new Intent(ProjectProgressActivity.this, OutListActivity.class).putExtra("budget_id", budget_id));
                                break;
                            case 5:
                                startActivity(new Intent(ProjectProgressActivity.this, ReceiveListActivity.class).putExtra("budget_id", budget_id));
                                break;
                            case 6:
                                startActivity(new Intent(ProjectProgressActivity.this, WuLiuListActivity.class).putExtra("budget_id", budget_id));
                                break;
                            case 7:
                                startActivity(new Intent(ProjectProgressActivity.this, YSListActivity.class).putExtra("budget_id", budget_id));
                                break;
                            case 8:
                                startActivity(new Intent(ProjectProgressActivity.this, YSListActivity.class).putExtra("budget_id", budget_id));
                                break;
                            case 9:
                                break;
                            case 10:
                                break;
                        }
                    }
                });
                holder.getImageView(R.id.tiao).setVisibility(View.VISIBLE);
                if ("4".equals(info1.get(position).getType())) {
                    holder.getImageView(R.id.tiao).setVisibility(View.GONE);
                }
                holder.getImageView(R.id.img).setImageResource(R.mipmap.ic_home_accomplish2x_li);
                holder.setTextView(R.id.name, info1.get(position).getName());
                holder.setTextView(R.id.result, info1.get(position).getResult());
                TextView res = holder.getTextView(R.id.result);
                if ("0".equals(info1.get(position).getColor())) {
                    res.setTextColor(getResources().getColor(R.color.color2));
                } else if ("1".equals(info1.get(position).getColor())) {
                    res.setTextColor(getResources().getColor(R.color.red));

                } else if ("2".equals(info1.get(position).getColor())) {
                    res.setTextColor(getResources().getColor(R.color.text179));
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
